package cn.edu.xjtu.android.emoji_warfare_android.game;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 数据管理器
 * 
 * @author bill
 *
 */
public class DataManager {
    public final static String TABLE_NAME = "score";
    public final static String[] COLUMNS = { "id", "mode", "score", "time" };
    private SQLiteDatabase db;

    public DataManager(Context context) {
        db = new OpenHelper(context).getWritableDatabase();
    }

    /**
     * 保存记录
     * 
     * @param score
     *            得分
     * @param mode
     *            模式
     * @param time
     *            时间
     * @return id
     */
    public long save(int score, String mode, long time) {
        ContentValues cv = new ContentValues();
        cv.put("score", score);
        cv.put("mode", (mode == null) ? "None" : mode);
        cv.put("time", time);
        return db.insert(TABLE_NAME, null, cv);
    }

    /**
     * 获取一条记录
     * 
     * @param id
     *            编号
     * @return 记录
     */
    public Record get(long id) {
        Cursor c = db.query(TABLE_NAME, COLUMNS, "id = ?", new String[] { String.valueOf(id) }, null, null, null);
        c.moveToFirst();
        Record r = new Record(id, c.getString(1), c.getInt(2), c.getLong(3));
        c.close();
        return r;
    }

    /**
     * 获取所有记录
     * 
     * @return 所有记录
     */
    public Record[] getAll() {
        return getTops(null);
    }

    /**
     * 获取前n条记录
     * 
     * @param count
     *            个数
     * @return 得分最高的若干记录
     */
    public Record[] getTops(int count) {
        return getTops(String.valueOf(count));
    }

    private Record[] getTops(String count) {
        Cursor c = db.query(TABLE_NAME, COLUMNS, null, null, null, null, "score desc", count);
        int n = c.getCount();
        c.moveToFirst();
        Record[] records = new Record[n];
        for (int i = 0; i < n; i++) {
            records[i] = new Record(c.getInt(0), c.getString(1), c.getInt(2), c.getLong(3));
            c.moveToNext();
        }
        c.close();
        return records;
    }

    /**
     * 记录数据类
     * 
     * @author bill
     *
     */
    public class Record {
        public final long id;
        public final String mode;
        public final int score;
        public final long time;

        public Record(long id, String mode, int score, long time) {
            this.id = id;
            this.mode = mode;
            this.score = score;
            this.time = time;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("Mode: ");
            sb.append(mode);
            sb.append(" Score: ");
            sb.append(score);
            sb.append(" Time: ");
            sb.append(time);
            return sb.toString();
        }
    }

    /**
     * 数据库工具
     * 
     * @author bill
     *
     */
    private class OpenHelper extends SQLiteOpenHelper {
        public final static String DATABASE_NAME = "emoji_warfare";
        public final static int VERSION = 1;
        public final static String CREATE_TABLE_VERSION_1 = "CREATE TABLE score(id INTEGER PRIMARY KEY, mode TEXT, score INTEGER, time LONG);";

        OpenHelper(Context context) {
            super(context, DATABASE_NAME, null, VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE_VERSION_1);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // 目前不存在升级操作
        }
    }
}
